/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package com.hefei.blog.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hefei.blog.admin.persist.service.AdminService;
import com.hefei.blog.web.controller.BaseController;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.frontend.framework.http.request.CommonParameterThreadLocal;

/**
 * 登录页
 */
@Controller
@RequestMapping("/admin")
public class AdminLoginController extends BaseController {
	
	private Logger logger = Logger.getLogger(AdminLoginController.class);

	@Autowired
	private AdminService adminService;
    /**
     * 跳转登录页
     * @return
     */
	@RequestMapping(value = "/login")
	public String view() {
		Long userId = CommonParameterThreadLocal.getUserId();
		if(userId != null)
			return "redirect:/admin";
		return "/admin/login";
	}

    /**
     * 提交登录
     * @param username
     * @param password
     * @param model
     * @return
     */
	@RequestMapping(value = "/vlogin")
	public String login(String username, String password,@RequestParam(value = "rememberMe",defaultValue = "0") int rememberMe, ModelMap model,
			HttpServletRequest request, HttpServletResponse response) {
		Long userId = CommonParameterThreadLocal.getUserId();
		if(userId != null)
			return "redirect:/admin";
		
		if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return "/admin/login";
        }
		String returnCode = adminService.login(username, password, request, response);
		if(!ReturnCode.RETURN_CODE_SUCCESS.equals(returnCode)){
			model.put("message", "用户名或密码错误");
			return "/admin/login";
		}else{
			return "redirect:/admin";
		}
	}
}
